#ifndef LOG_H
#define	LOG_H

#include <syslog.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>

#define USE_LOG

#ifdef USE_LOG

void Log(int level,const char* format,...);
void Log_msg(int level,const char* message);

#define LOGGER(type,id) LOGGER_##type(id)

#define LOGGER_CONSOLE(id) \
void init_logger() {} \
void Log(int level,const char *format, ...) { \
  va_list argptr; \
  va_start(argptr, format); \
  time_t ltime; \
  time( &ltime ); \
  struct tm now; \
  localtime_r( &ltime, &now); \
  fprintf(stderr,"%s: ",id); \
  fprintf(stderr,"%4u-%02u-%02u %02u:%02u:%02u L%i: ", \
		now.tm_year+1900,now.tm_mon+1, now.tm_mday, now.tm_hour, \
		now.tm_min, now.tm_sec, level); \
  vfprintf(stderr,format, argptr); \
	fprintf(stderr,"\n"); \
  va_end(argptr); \
} \
COMMON

#define LOGGER_SYSLOG(id) \
void init_logger(const char* ident) { \
	openlog(id,LOG_PID,LOG_USER); \
} \
void Log(int level,const char *format, ...) \
{ \
  va_list argptr; \
  va_start(argptr, format); \
  vsyslog(LOG_MAKEPRI(LOG_USER, level), format, argptr); \
  va_end(argptr); \
} \
COMMON

#define COMMON \
void Log_msg(int level,const char* message) { \
    Log(level,"%s",message); \
}

#else //USE_LOG

#define LOGGER(type,id)
#define init_logger(id)
#define Log(level,format,...)
#define Log_msg(level,message)

#endif // USE_LOG

#endif	/* LOG_H */

